package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.AdminRoleDao;
import com.dao.RoleDao;
import com.pojo.AdminRole;
import com.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@Service
public class AdminRoleService extends ServiceImpl<AdminRoleDao, AdminRole> {
    @Autowired
    private AdminRoleDao adminRoleMapper;

    @Autowired
    private RoleDao roleDAO;

    /**
     * 根据用户ID查用户的所有角色
     * @param id
     * @return
     */
    public List<String> shifouGuanliyuan(String id){
        return adminRoleMapper.shifouGuanliyuan(id);
    }

    public IPage findRoleByAdmin(int page, int size, String text) {
        IPage iPage = new Page(page, size);
        IPage<Map> maps = adminRoleMapper.findRoleByAdmin(iPage, text);
        return maps;
    }

    public Map findAllRole(String adminId) {
        Map map = new HashMap();
        List all = roleDAO.getNotRolesByAdmin(adminId);//所有可用的角色

        List<Role> adminRole = roleDAO.getRolesByAdmin(adminId);

        map.put("all",all);
        map.put("admin",adminRole);

        return map;

    }

    public Map adminRoleSave(String adminId, String roleId) {
        AdminRole adminRole = new AdminRole();
        //int adminIdInt = Long.parseInt(adminId);
        //int roleIdInt = Integer.parseInt(roleId);
        adminRole.setAdminId(adminId);
        adminRole.setRoleId(roleId);
        int n = adminRoleMapper.insert(adminRole);
        Map map = new HashMap();
        map.put("n", n);
        map.put("id", adminRole.getAdminRoleId());
        return map;
    }

    public int adminRoleDel(String adminId,String adminRoleId) {
        QueryWrapper q = new QueryWrapper();
        q.eq("admin_id",adminId);
        q.eq("role_id",adminRoleId);
        int n = adminRoleMapper.delete(q);
        return n;
    }
}
